{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2bf735a6-1b46-4c14-900a-3a8fa8e56f71",
   "metadata": {},
   "source": [
    "# while  循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15788d2e-714c-4de9-b9b7-7c73ca0cca7e",
   "metadata": {},
   "source": [
    "![img](https://www.runoob.com/wp-content/uploads/2015/12/loop.png)while  循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fad533f-fae2-4869-8106-731ab2149267",
   "metadata": {},
   "source": [
    "``` python\n",
    "while 判断条件(condition)：    # 判断条件是布尔类型   TRUE False  或者  0  或 非零\n",
    "    执行语句(statements)……   # 这里面 一定要有可以改变条件的语句 不然循环永远出不去了\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "19d01404-fdf8-4123-a159-fe855b1dc806",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "n = 100\n",
    "sums = 0\n",
    "counter = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "3f2e9878-f64b-4fca-9afe-62b947b6dfde",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "while counter <= n:\n",
    "    sums = sums + counter\n",
    "    counter += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "54f63d0b-d5b1-47a7-8d0b-3b58d580cce9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 到 100 之和为: 5050\n"
     ]
    }
   ],
   "source": [
    "print(\"1 到 %d 之和为: %d\" % (n,sum))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "137287ad-d77f-428f-b4c5-9c2f471ea690",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "while counter <= n:\n",
    "    sums += counter\n",
    "    counter += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "61a763c1-cd2c-499f-98c0-8bdd096cdbf9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个数字  : 123\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的数字是:  123\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个数字  : 14\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的数字是:  14\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个数字  : 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的数字是:  1\n",
      "Good bye!\n"
     ]
    }
   ],
   "source": [
    "var = 0\n",
    "while var != 1 : #  1 == 1 表达式永远为 true      1 == 1 这个必然成立\n",
    "    var = int(input(\"输入一个数字  :\"))\n",
    "    print(\"你输入的数字是: \", var) \n",
    "print (\"Good bye!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "888ad51e-b570-494b-b708-519d0f27a99c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dd54a79-c00b-4ecc-bed5-59e120a778ec",
   "metadata": {
    "tags": []
   },
   "source": [
    "``` python\n",
    "while <expr>:\n",
    "    <statement(s)>\n",
    "else:\n",
    "    <additional_statement(s)>    # 执行完while 后执行   重点：执行完\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "9f906e2f-7dbf-45f6-b8f5-e188fdb4477f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0  小于 5\n",
      "1  小于 5\n",
      "2  小于 5\n",
      "3  小于 5\n",
      "4  小于 5\n",
      "5  大于或等于 5\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while count < 5:\n",
    "    print (count, \" 小于 5\")\n",
    "    count = count + 1\n",
    "    # break\n",
    "else:\n",
    "    print (count, \" 大于或等于 5\")   # 很少用到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4585b9b1-d972-45f4-9547-9d8c0ffa8a12",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
